package android.framework.widget.gallery;

import java.lang.reflect.Method;
import java.util.Timer;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

/**
 * @ClassName: HomeImageGallery2
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-5-10 ����9:23:53
 * 
 */
public class AutoFlingGallery extends Gallery {

	private int width;

	public AutoFlingGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		width = getResources().getDisplayMetrics().widthPixels;
		setAnimationDuration(200);
	}

	public AutoFlingGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		width = getResources().getDisplayMetrics().widthPixels;
		setAnimationDuration(200);
	}

	public AutoFlingGallery(Context context) {
		super(context);
		width = getResources().getDisplayMetrics().widthPixels;
		setAnimationDuration(200);
	}

	// /**
	// *
	// * @Title: cancelAutoSlide
	// * @Description: ȡ���Զ���
	// */
	// public void cancelAutoSlide() {
	// isAutoSlideMode = false;
	// // if (this.timer == null)
	// // return;
	// // this.timer.cancel();
	// // this.timer = null;
	// }

	private boolean isAutoSlideMode = true;

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1 == null || e2 == null) {
			return super.onFling(e1, e2, velocityX, velocityY);
		} else if (e2.getRawX() - e1.getRawX() <= 0) {
			// Message localMessage = Message.obtain();
			// handler.sendMessage(localMessage);
			onKeyDown(22, null);
		} else {
			// Message localMessage = Message.obtain();
			// localMessage.what = MSG_SLIDE_LEFT;
			// handler.sendMessage(localMessage);
			onKeyDown(21, null);
		}
		return true;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		// cancelAutoSlide();
		this.isAutoSlideMode = false;
		this.handler.removeMessages(MSG_SLIDE_RIGHT);
		return super.onDown(e);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// if (event.getAction() == MotionEvent.ACTION_DOWN) {
		// cancelAutoSlide();
		// } else

		if (event.getAction() == MotionEvent.ACTION_UP) {
			this.isAutoSlideMode = true;
			// startAutoSlide();
			Message msg = handler.obtainMessage(MSG_SLIDE_RIGHT);
			this.handler.sendMessageDelayed(msg, PERIOD);
		}
		return super.onTouchEvent(event);
	}

	// /**
	// * ��ʱ��
	// */
	// private TimerTask task = new TimerTask() {
	// @Override
	// public void run() {
	// if (isAutoSlideMode) {
	// Message localMessage = Message.obtain();
	// localMessage.what = MSG_SLIDE_LEFT;
	// handler.sendMessage(localMessage);
	// }
	// }
	// };

	private long PERIOD = 5000l;// �Զ���ʱ����
	private Timer timer; // ��ʱ��
	private static final int MSG_SLIDE_LEFT = 1;
	private static final int MSG_SLIDE_RIGHT = 2;

	Handler handler = new Handler() {
		public void handleMessage(Message paramMessage) {
			super.handleMessage(paramMessage);
			switch (paramMessage.what) {
			case MSG_SLIDE_RIGHT:
				if (isAutoSlideMode) {
					// onKeyDown(21, null);
					onFling(null, null, 2.1F * -width, 0.0F);
					Message msg = handler.obtainMessage(MSG_SLIDE_RIGHT);
					handler.sendMessageDelayed(msg, PERIOD);
				}
				break;
			}

		}
	};

	/**
	 * @Title: startAutoSlide
	 * @Description: ��ʼ��
	 */
	public void startAutoSlide() {
		this.isAutoSlideMode = true;
		// if (this.timer == null) {
		// this.timer = new Timer();
		// this.task = null;
		// this.task = new TimerTask() {
		// public void run() {
		// if (isAutoSlideMode) {
		// Message localMessage = Message.obtain();
		// localMessage.what = MSG_SLIDE_RIGHT;
		// handler.sendMessage(localMessage);
		// }
		// }
		// };
		// }
		// this.timer.schedule(this.task, PERIOD, PERIOD);
		handler.sendEmptyMessageDelayed(MSG_SLIDE_RIGHT, PERIOD);
	}

	/**
	 * @Title: fling
	 * @Description: �Զ���
	 * @param methodName
	 *            �����
	 */
	private void slide(String methodName) {
		try {
			@SuppressWarnings("unchecked")
			Class<Gallery> c = ((Class<Gallery>) Class
					.forName("android.widget.Gallery"));
			Method[] ms = c.getDeclaredMethods();
			for (Method m : ms) {
				if (m.getName().equals(methodName)) {
					m.setAccessible(true);
					m.invoke(this);
				}
			}
		} catch (Exception e) {

		}
	}

}
